package com.ruoyi.system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ruoyi.system.pojo.user.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * Description:
 *
 * @author JourWon
 * @date Created on 2025/5/13
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {
    User selectUserByUserName(String loginName);

    User selectUserByMobile(String mobile);

    @Select("<script>" +
            "SELECT COUNT(*) FROM tb_user u " +
            "LEFT JOIN tb_role r ON u.role_id = r.role_id " +
            "<where>" +
            "   <if test='userName != null and userName != \"\"'>" +
            "       AND u.user_name LIKE CONCAT('%', #{userName}, '%')" +
            "   </if>" +
            "   <if test='roleId != null'>" +
            "       AND u.role_id = #{roleId}" +
            "   </if>" +
            "   <if test='isRepair != null'>" +
            "       AND r.role_code = #{isRepair ? '1003' : '1002'}" +
            "   </if>" +
            "</where>" +
            "</script>")
    int countByCondition(@Param("userName") String userName, @Param("roleId") Integer roleId, @Param("isRepair") Boolean isRepair);

    @Select("<script>" +
            "SELECT u.* FROM tb_user u " +
            "LEFT JOIN tb_role r ON u.role_id = r.role_id " +
            "<where>" +
            "   <if test='userName != null and userName != \"\"'>" +
            "       AND u.user_name LIKE CONCAT('%', #{userName}, '%')" +
            "   </if>" +
            "   <if test='roleId != null'>" +
            "       AND u.role_id = #{roleId}" +
            "   </if>" +
            "   <if test='isRepair != null'>" +
            "       AND r.role_code = #{isRepair ? '1003' : '1002'}" +
            "   </if>" +
            "</where>" +
            "LIMIT #{offset}, #{pageSize}" +
            "</script>")
    List<User> searchByPage(@Param("userName") String userName, @Param("roleId") Integer roleId, @Param("isRepair") Boolean isRepair, @Param("offset") int offset, @Param("pageSize") Integer pageSize);
}
